package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.Agent;
import tech.aistar.entity.AppointmentTime;
import tech.aistar.entity.Result;
import tech.aistar.entity.Tenant;
import tech.aistar.service.IAgentService;
import tech.aistar.service.IAppointmentTimeService;
import tech.aistar.service.ITenantService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/Appointment")
public class AppointmentController {
    @Autowired
    private IAppointmentTimeService appointmentTimeService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IAgentService agentService;


    @GetMapping("/TenantApp")
    public String select(HttpServletRequest request) {
        String tenantId = request.getParameter("tenantId");
        Tenant tenant = tenantService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("tenant", tenant);
        List<AppointmentTime> appointmentTime = appointmentTimeService.selectByTenantId(Integer.valueOf(tenantId));
        request.setAttribute("appointmentTime", appointmentTime);
        List<Agent> agents = new ArrayList<>();
        for (int i = 0; i < appointmentTime.size(); i++) {
            Agent agent = agentService.selectById(appointmentTime.get(i).getAgentId());
            agents.add(agent);
        }
        request.setAttribute("agent", agents);
        return "/personal_homepage/tenant/appointment";
    }

    @PostMapping("/delApp")
    public @ResponseBody
    Result deleteById(Integer id) {
        int count = appointmentTimeService.deleteById(id);

        if (count == 1) {
            Result result = new Result("200", "取消成功");
            return result;
        } else {
            Result result = new Result("500", "取消失败");
            return result;
        }
    }


    @PostMapping("/changeTime")
    public @ResponseBody Result updateById(Integer id, String appointmentDates, String timeQuantum, String agentId,Integer tenantId) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date appointmentDate = sdf.parse(appointmentDates);
        AppointmentTime appointmentTime = appointmentTimeService.selectById(id);
        Date date = appointmentTime.getAppointmentDate();
        String app = sdf.format(date);
        String time = appointmentTime.getTimeQuantum();
        String a = app.concat("/");
        String apptime = a.concat(time);
        AppointmentTime appointmentTimes = appointmentTimeService.selectByAppointmentDate(appointmentDate,timeQuantum,Integer.valueOf(agentId));
        AppointmentTime appointmentTimess = appointmentTimeService.selectByAppointmentDates(appointmentDate,timeQuantum,tenantId);
        if (appointmentTimes !=null || appointmentTimess !=null) {
            return new Result("500","更改失败",apptime);
        } else {
            int count = appointmentTimeService.updateById(appointmentDate, timeQuantum, id);
            if (count == 1) {
                return new Result("200", "修改成功");
            } else {
                return new Result("500", "修改失败", apptime);
            }
        }

    }
    @PostMapping("/nochangeTime")
    public @ResponseBody Result selectById(Integer id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        AppointmentTime appointmentTime = appointmentTimeService.selectById(id);
        Date date = appointmentTime.getAppointmentDate();
        String app = sdf.format(date);
        String time = appointmentTime.getTimeQuantum();
        String a = app.concat("/");
        String apptime = a.concat(time);
        return new Result("200", "取消成功",apptime);
    }




    @GetMapping("/AgentApp")
    public String selectByAgentId(HttpServletRequest request) {
        String agentId = request.getParameter("agentId");
        Agent agent = agentService.selectById(Integer.valueOf(agentId));
        request.setAttribute("agent", agent);
        List<AppointmentTime> appointmentTime = appointmentTimeService.selectByAgentId(Integer.valueOf(agentId));
        request.setAttribute("appointmentTime", appointmentTime);
        List<Tenant> tenants = new ArrayList<>();
        for (int i = 0; i < appointmentTime.size(); i++) {
            Tenant tenant = tenantService.selectById(appointmentTime.get(i).getTenantId());
            tenants.add(tenant);
        }
        request.setAttribute("tenant", tenants);
        return "/personal_homepage/broker/myappointment";
    }



    @PostMapping("/changeTrue")
    public @ResponseBody Result StatetrueById(Integer id) {
        int count = appointmentTimeService.updateStateById(id);
        if (count==1){
            return new Result("200","确认成功");
        }else{
            return new Result("500","确认失败");
        }

    }

    @PostMapping("/changeFalse")
    public @ResponseBody Result StatefalseById(Integer id) {
        int count = appointmentTimeService.updateStatefById(id);
        if (count==1){
            return new Result("200","取消成功");
        }else{
            return new Result("500","取消失败");
        }

    }




}
